package com.sky.aspect;

import com.sky.result.Result;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
@Slf4j
@Aspect
@Component
public class LogContollerAspect {

    @Before("execution(* com.sky.controller.admin.*.*(..))")
    public void logStart(JoinPoint joinPoint) {
        // 获取方法签名
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        // 获取 @ApiOperation 注解
        ApiOperation apiOperation = method.getAnnotation(ApiOperation.class);
        // 获取注解的 value 属性
        String value = apiOperation.value();
        // 获取方法的参数
        Object[] args = joinPoint.getArgs();
        if (args == null || args.length == 0) {
            args = new Object[]{"无参数"};
        }
        log.info("【{}】的方法其参数为:{}",value,args);
    }
    @AfterReturning(pointcut = "execution(* com.sky.controller.admin.*.*(..))",returning = "result")
    public void logRetrun(JoinPoint joinPoint, Result result) {
        // 获取方法签名
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        // 获取 @ApiOperation 注解
        ApiOperation apiOperation = method.getAnnotation(ApiOperation.class);
        // 获取注解的 value 属性
        String value = apiOperation.value();
        log.info("【{}】的方法其返回值为:{}",value,result);
    }
}
